package com.lzb.compressmaster.utils;

/**
 * 图片压缩配置构建器
 * 提供链式调用的方式来构建压缩配置
 */
public class CompressConfigBuilder {
    
    private ImageCompressor.CompressConfig config;

    /**
     * 智能压缩配置
     */
    public static CompressConfigBuilder smart() {
        return new CompressConfigBuilder(ImageCompressor.CompressType.SMART);
    }

    /**
     * 高级压缩配置（按百分比缩放）
     */
    public static CompressConfigBuilder advanced() {
        return new CompressConfigBuilder(ImageCompressor.CompressType.ADVANCED);
    }

    /**
     * 指定大小压缩
     */
    public static CompressConfigBuilder size(long targetSizeBytes) {
        return new CompressConfigBuilder(ImageCompressor.CompressType.SIZE)
                .setTargetSize(targetSizeBytes);
    }

    /**
     * 指定像素压缩
     */
    public static CompressConfigBuilder pixel(int width, int height) {
        return new CompressConfigBuilder(ImageCompressor.CompressType.PIXEL)
                .setWidth(width)
                .setHeight(height);
    }

    private CompressConfigBuilder(ImageCompressor.CompressType type) {
        this.config = new ImageCompressor.CompressConfig(type);
    }

    /**
     * 设置输出格式
     */
    public CompressConfigBuilder setFormat(ImageCompressor.OutputFormat format) {
        config.setOutputFormat(format);
        return this;
    }

    /**
     * 设置质量（1-100）
     */
    public CompressConfigBuilder setQuality(int quality) {
        config.setQuality(quality);
        return this;
    }

    /**
     * 设置目标大小（字节）
     */
    public CompressConfigBuilder setTargetSize(long targetSizeBytes) {
        config.setTargetSize(targetSizeBytes);
        return this;
    }

    /**
     * 设置目标宽度
     */
    public CompressConfigBuilder setWidth(int width) {
        config.setWidth(width);
        return this;
    }

    /**
     * 设置目标高度
     */
    public CompressConfigBuilder setHeight(int height) {
        config.setHeight(height);
        return this;
    }

    /**
     * 设置缩放百分比（高级压缩使用）
     */
    public CompressConfigBuilder setScalePercent(float percent) {
        config.setScalePercent(percent);
        return this;
    }

    /**
     * 设置是否保持宽高比
     */
    public CompressConfigBuilder keepAspectRatio(boolean keep) {
        config.setKeepAspectRatio(keep);
        return this;
    }

    /**
     * 设置自定义后缀
     */
    public CompressConfigBuilder setSuffix(String suffix) {
        config.setCustomSuffix(suffix);
        return this;
    }

    /**
     * 构建配置
     */
    public ImageCompressor.CompressConfig build() {
        return config;
    }

    /**
     * 常用配置预设
     */
    public static class Presets {
        
        /**
         * 微信分享优化配置
         */
        public static ImageCompressor.CompressConfig wechatShare() {
            return CompressConfigBuilder.pixel(1080, 1080)
                    .setFormat(ImageCompressor.OutputFormat.JPG)
                    .setQuality(80)
                    .keepAspectRatio(true)
                    .setSuffix("_wechat")
                    .build();
        }

        /**
         * 头像压缩配置
         */
        public static ImageCompressor.CompressConfig avatar() {
            return CompressConfigBuilder.pixel(512, 512)
                    .setFormat(ImageCompressor.OutputFormat.JPG)
                    .setQuality(85)
                    .keepAspectRatio(true)
                    .setSuffix("_avatar")
                    .build();
        }

        /**
         * 缩略图配置
         */
        public static ImageCompressor.CompressConfig thumbnail() {
            return CompressConfigBuilder.pixel(200, 200)
                    .setFormat(ImageCompressor.OutputFormat.JPG)
                    .setQuality(75)
                    .keepAspectRatio(true)
                    .setSuffix("_thumb")
                    .build();
        }

        /**
         * 高清保存配置
         */
        public static ImageCompressor.CompressConfig highQuality() {
            return CompressConfigBuilder.advanced()
                    .setScalePercent(90f)
                    .setFormat(ImageCompressor.OutputFormat.JPG)
                    .setQuality(95)
                    .keepAspectRatio(true)
                    .setSuffix("_hq")
                    .build();
        }

        /**
         * 极限压缩配置
         */
        public static ImageCompressor.CompressConfig extreme() {
            return CompressConfigBuilder.size(100 * 1024) // 100KB
                    .setFormat(ImageCompressor.OutputFormat.JPG)
                    .setQuality(70)
                    .keepAspectRatio(true)
                    .setSuffix("_mini")
                    .build();
        }
    }
}